package com.dsfa.biz.dataCenter;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.dsfa.platform.starter.db.jfinal.plugin.activerecord.Record;
import com.dsfa.util.TimeUtil;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: TODO
 * @ClassName: AgentLogWSDto
 * @Author: wangdm
 * @Date: 2024/1/25
 */
@Data
public class AgentLogWSDto {
    //单表主键 24012510352061488642790319f536d
    private String id;
    //请求时间  req_time
    private java.util.Date reqTime;
    //请求的ip req_ip
    private String reqIp;
    //请求地址  req_url
    private String reqUrl;
    //请求的目标服务地址 req_interface_url
    private String reqInterfaceUrl;
    //请求的报文 req_data
    private String reqData;
    //验证是否通过，默认0不通过 valid
    private Integer valid;
    //验证信息  valid_msg
    private String validMsg;
    //应用code    app_code
    private String appCode;
    //应用key app_key
    private String appKey;
    //应用名称  app_name
    private String appName;
    //请求的服务接口   req_server_interface_id
    private String reqServerInterfaceId;
    //请求的服务接口 req_server_interface_method
    private String reqServerInterfaceMethod;
    //请求的服务接口名称 req_server_interface_name
    private String reqServerInterfaceName;
    //返回的HTTP状态 rep_status
    private String repStatus;
    //返回的报文 rep_data
    private String repData;
    //返回头信息 rep_header
    private String repHeader;
    //备注    remark
    private String remark;
    //删除标识  rowstate
    private Integer rowstate;
    //创建时间  create_time
    private java.util.Date createTime;
    //所属产品  product_id
    private String productId;
    //请求方式  request_type_text
    private String requestTypeText;
    //响应时长  resp_time
    private Integer respTime;
    //请求结束时间    req_end_time
    private java.util.Date reqEndTime;
    //请求开始时间，不入库
    private long timeStart;

    public AgentLogWSDto(String timestamp, HttpServletRequest request) {
        long time = System.currentTimeMillis();
        this.setId(TimeUtil.convertTime(time, TimeUtil.PatternJoin) +
                DigestUtils.md5Hex(time + timestamp).substring(13));
        this.setReqTime(DateTime.now());//请求时间
        this.setReqIp(request.getRemoteAddr());//请求的ip
        this.setReqUrl(request.getRequestURL().toString() + "?" + request.getQueryString());//请求地址
        //this.setReqData((String) message.get(Message.QUERY_STRING)+"input");
        this.setRequestTypeText(request.getMethod());//请求方式
        this.setCreateTime(DateTime.now());//开始时间
        this.setTimeStart(time);//开始时间戳，用于计算时长

    }

    public AgentLogWSDto(JSONObject vjs, String paramJSON, HttpServletRequest request) {
        long time = System.currentTimeMillis();
        this.id = TimeUtil.convertTime(time, TimeUtil.PatternJoin) +
                DigestUtils.md5Hex(time + vjs.getString("timestamp")).substring(13);
        this.reqTime = DateTime.now();//请求时间
        this.reqIp = request.getRemoteAddr();//请求的ip
        this.reqUrl = request.getRequestURL().toString() + "?" + request.getQueryString();//请求地址
        this.requestTypeText = request.getMethod();//请求方式
        this.reqData = vjs.toJSONString() + "  " + paramJSON;
        this.reqServerInterfaceId = vjs.getString("id");
        this.appCode = vjs.getString("app");
        this.productId = vjs.getString("app");//TODO 所属产品 暂时写appcode
        this.createTime = DateTime.now();//请求方式
        this.timeStart = time;//开始时间戳，用于计算时长
    }

    public void setReqInterface(Record r) {
        this.reqInterfaceUrl = r.getStr("targetUrl");
        this.reqServerInterfaceMethod = r.getStr("interfaceMethod");//接口
        this.reqServerInterfaceName = r.getStr("interfaceName");//接口中文名
    }

    public void setResData(HttpServletResponse response, String resData, String verifyRes) {
        this.repData = resData;
        this.valid = "ok".equals(verifyRes) ? 1 : 0;//验证是否通过
        this.repStatus = response.getStatus() + "";
        this.repHeader = response.toString();//验证是否通过
        this.respTime = (int) ((System.currentTimeMillis() - this.timeStart) / 1000);
        this.reqEndTime = DateTime.now();
    }

    /**
     * @Description: 创建时转化为Record
     * @Author: wangdm
     * @Date: 2024/1/25
     */
    public Record toAddRecord() {
        Record r = new Record();
        r.set("dgp_dq_agent_ws_log_id", this.id)
                .set("req_time", this.reqTime)
                .set("req_ip", this.reqIp)
                .set("req_url", this.reqUrl)
                .set("req_interface_url", this.reqInterfaceUrl)
                .set("req_data", this.reqData)
                .set("valid_value", this.valid)
                .set("valid_text", this.valid == 1 ? "是" : "否")
                .set("valid_msg", this.validMsg)
                .set("app_code", this.appCode)
                .set("app_key", this.appKey)
                .set("app_name", this.appName)
                .set("req_server_interface_id", this.reqServerInterfaceId)
                .set("req_server_interface_method", this.reqServerInterfaceMethod)
                .set("req_server_interface_name", this.reqServerInterfaceName)
                .set("rep_status", this.repStatus)
                .set("rep_data", this.repData)
                .set("rep_header", this.repHeader)
                .set("remark", this.remark)
                .set("ds_create_time", this.createTime)
                .set("product_id", this.productId)
                .set("request_type_text", this.requestTypeText)
                .set("resp_time", this.respTime)
                .set("req_end_time", this.reqEndTime)
        ;
        return r;
    }
}
